<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '' }">版本管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/app_version' }"
        >版本列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>修改版本信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form
          :model="form"
          :rules="rules"
          label-width="80px"
          ref="form"
          style=" width: 400px;"
        >
          <el-form-item label="版本名称" prop="versionName">
            <el-input v-model="form.versionName"></el-input>
          </el-form-item>
          <el-form-item label="版本号" prop="versionCode">
            <el-input v-model="form.versionCode"></el-input>
          </el-form-item>
          <el-form-item label="版本信息" prop="project">
            <el-input v-model="form.project"></el-input>
          </el-form-item>
          <el-form-item label="系统类型" prop="osType">
            <el-select placeholder="请选择系统类型" v-model="form.osType">
              <el-option
                :key="item.code"
                :label="item.desc"
                :value="item.code"
                v-for="item in osTypeList"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="sign密码" prop="signPwd">
            <el-input v-model="form.signPwd"></el-input>
          </el-form-item>
          <el-form-item label="版本简介" prop="remark">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即创建
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        versionName: "",
        versionCode: "",
        osType: null,
        signPwd: "",
        project: "",
        remark: ""
      },
      rules: {
        versionName: [
          { required: true, message: "请输入版本名称", trigger: "none" }
        ],
        versionCode: [
          { required: true, message: "请输入版本号", trigger: "none" }
        ]
      },
      osTypeList: [
        { code: 1, desc: "安卓" },
        { code: 2, desc: "IOS" },
        { code: 3, desc: "其他" }
      ]
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
  },
  methods: {
    info() {
      this.$ajax.app_version
        .info(this.id)
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
            this.$router.push("/app_version/");
          } else {
            this.form = result.data;
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.app_version
            .update(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "提交成功!"
                });
                this.$router.push("/app_version/");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/app_version/");
    }
  }
};
</script>
